﻿using System;
using System.Reflection;
using System.Windows;
using Microsoft.Practices.Unity;
using WpfApplication6.Model;
using WpfApplication6.View;
using WpfApplication6.ViewModel;

namespace WpfApplication6
{
    public class ShellCommandAttribute : Attribute
    {
    }

    /// <summary>
    ///     Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            IUnityContainer container = new UnityContainer();

            container.RegisterType<INorthwindDbContext, NorthwindDbContext>(new ContainerControlledLifetimeManager());
            container.RegisterType<IProductsRepository, ProductsRepository>(new ContainerControlledLifetimeManager());

            var view = new MainWindow();
            var viewModel = new MainWindowViewModel();
            view.DataContext = viewModel;

            container.RegisterInstance<IShell>(viewModel);

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                object[] attributes = type.GetCustomAttributes(true);
                foreach (object attribute in attributes)
                {
                    if (attribute is ShellCommandAttribute)
                    {
                        viewModel.Commands.Add((CommandViewModel) container.Resolve(type));
                    }
                }
            }

            //viewModel.Commands.Add(container.Resolve<OneCommandViewModel>());
            //viewModel.Commands.Add(container.Resolve<ShowProductsViewModel>());

            view.Show();
        }
    }
}